package purchase.web;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;

public class PurchaseActionServlet implements Servlet {
	
	private static Log log = LogFactory.getLog(PurchaseActionServlet.class);
	
	private JbpmContext jbpmContext;
	
	public ServletConfig getServletConfig() {
		return this.getServletConfig();
	}

	public String getServletInfo() {
		return this.getServletInfo();
	}

	public void init(ServletConfig servletConfig) throws ServletException {
		log.info("PurchaseActionServlet init......");
	}

	/**
	 * 编码设置
	 * @param request
	 * @param response
	 */
	private void setReqRespEncoding(ServletRequest request,
			ServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
	}
	
	/**
	 * 接受请求的服务
	 */
	public void service(ServletRequest req, ServletResponse resp)
			throws ServletException, IOException {
		setReqRespEncoding(req,resp);
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)resp;
		
		//creat jbpmContext object. 
		jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
		log.info("create JbpmContext object......");
		
		//如:/jbpm_web_test/aaa.do
		String requestUri = request.getRequestURI();
		//如：/jbpm_web_test
		String contextPath = request.getContextPath();
		
		//handler
		PurchaseActionServletHandler purchaseActionServletHandler = 
			new PurchaseActionServletHandler(request, response,jbpmContext);
		
		try {
			if (requestUri.equals(contextPath+"/purchase/login.do")) {//...
				purchaseActionServletHandler.login();
			} else if (requestUri.equals(contextPath+"/purchase/myTaskList.do")) {//...
				purchaseActionServletHandler.myTaskList();
			} else if (requestUri.equals(contextPath+"/purchase/inputPurchase.do")) {//...
				purchaseActionServletHandler.inputPurchase();
			} else if (requestUri.equals(contextPath+"/purchase/createPurchase.do")) {//...
				purchaseActionServletHandler.createPurchase();
			} else if (requestUri.equals(contextPath+"/purchase/inputMgrApprovePurchase.do")) {//...
				purchaseActionServletHandler.inputMgrApprovePurchase();
			} else if (requestUri.equals(contextPath+"/purchase/mgrApprovePurchase.do")) {//...
				purchaseActionServletHandler.mgrApprovePurchase();
			} else if (requestUri.equals(contextPath+"/purchase/inputCancelPurchase.do")) {//...
				purchaseActionServletHandler.inputCancelPurchase();
			} else if (requestUri.equals(contextPath+"/purchase/cancelPurchase.do")) {//...
				purchaseActionServletHandler.cancelPurchase();
			} else if (requestUri.equals(contextPath+"/purchase/inputModifyPurchase.do")) {//...
				purchaseActionServletHandler.inputModifyPurchase();
			} else if (requestUri.equals(contextPath+"/purchase/modifyPurchase.do")) {//...
				purchaseActionServletHandler.modifyPurchase();
			} 
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
			//close connection jbpmContext
			try {
				jbpmContext.getConnection().close();
				log.info("close jbpmContext.connection object......");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			jbpmContext.close();
			
			log.info("close JbpmContext object......");
		}
		
	}

	public void destroy() {
		log.info("PurchaseActionServlet destroy......");
	}
}
